<?php
namespace app\bancard\controller;

use think\Db;
use think\facade\Cache;
use app\common\validate\DemeanorValidate;
use app\bancard\model\CampusElegance as DemeanorModel;

class Demeanor extends Common
{
    	//推送key;
	protected $pushKey = [
        'action' => 'refresh',
        'params' => 'demeanor',
    ];
    
    //首页
    public function index()
    {
        return $this->fetch();
    }
    //列表
    public function lists()
    {
        $type = request()->param('type');
        $ntitle =request()->param('keywords');
        $title = isset($ntitle)?$ntitle:'';
        $limit = request()->param('limit');
        $offset = request()->param('offset');
        $start = ($offset-1)*$limit;
        $user_id = session('admin.id');
        $school_id = session('schoolInfo.id');
        $map =[
            'type'=>$type,
            'user_id'=>$user_id,
            'school_id'=>$school_id
        ];
        if ($title) {
            $count = db('campus_elegance')->where($map)->whereLike('title', "%$title%")->count();
            $result = db('campus_elegance')->where($map)->whereLike('title', "%$title%")->limit($start, $limit)->order('id desc,sort desc')->select();
        } else {
            $count = db('campus_elegance')->where($map)->count();
            $result = db('campus_elegance')->where($map)->limit($start, $limit)->order('id desc,sort desc')->select();
        }
        
        
        
        if ($type == 1) {
            foreach ($result as $k=>$v) {
                if (!empty($v['images'])) {
                    $n = explode(',', $v['images']);
                    foreach ($n as $key=>$value) {
                        $result[$k]['pics'][$key]['thumb'] = $value;
                        $result[$k]['pics'][$key]['src'] = $value;
                        $result[$k]['pics'][$key]['pid'] = $k.$key;
                    }
                }
            }
        }
        return showTable(0, 'OK', $count, $result, 200);
    }
    
    //查看
    public function view()
    {
        $data = request()->param();
        $result = db('campus_elegance')->where('id', $data['id'])->find();
        $beforePage = $this->beforePage($data);
        $afterPage = $this->afterPage($data);
        $this->assign('beforePage', $beforePage);
        $this->assign('afterPage', $afterPage);
        $this->assign('result', $result);
        return $this->fetch();
    }
    
    //下一篇
    public function beforePage($data)
    {
        $id = request()->param('id');
        $result = db('campus_elegance')->where('id', '>', $id)->limit(1)->find();
        if (empty($result)) {
            $result   = "上一篇:<a href='javascript:;'>没有了</a>";
        } else {
            $url = url('bancard/Activitys/view', ['id'=>$result['id']]);
            $result = "上一篇:<a href='".$url."' class='before'>".mb_substr($result['title'], 0, 20, 'utf-8')."</a>";
        }
        return $result;
    }
    //上一篇
    public function afterPage($data)
    {
        $id = request()->param('id');
        $result = db('campus_elegance')->where('id', '<', $id)->limit(1)->order('id', 'desc')->find();
        
        if (empty($result)) {
            $result   = "下一篇:<a href='javascript:;'>没有了</a>";
        } else {
            $url = url('bancard/Demeanor/view', ['id'=>$result['id']]);
            $result = "下一篇:<a href='".$url."' class='after'>".mb_substr($result['title'], 0, 20, 'utf-8')."</a>";
        }
        return $result;
    }
    //图片编辑
    public function edit()
    {
		if (request()->isPost()) {
		    $data = request()->param();
		    $data['user_id'] = session('admin.id');
		    $data['school_id'] = session('schoolInfo.id');
		    $data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
			$data['images'] =  implode(",", $data['images']);
		    $validate = new DemeanorValidate;
		    if (!$validate->scene('add')->check($data)) {
		        return show(0, $validate->getError(), 0, 200);
		    } else {
		        Cache::clear('campus_elegance');
                $rest = (new DemeanorModel())->strict(false)->where('id',$data['id'])->update($data);
                if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
		        return show(1, '添加成功', $rest, 200);
		    }
		}
        $data = request()->param();
        $result = db('campus_elegance')->where('id', '=', $data['id'])->find();
		$egetClass = $this->egetClass($result['class_ids']);
		$this->assign('egetClass', json_encode($egetClass['data']));
        $this->assign('result', $result);
        return $this->fetch();
    }
	//视频编辑
	public function editVideo()
	{
		if (request()->isPost()) {
		    $data = request()->param();
		    $data['user_id'] = session('admin.id');
		    $data['school_id'] = session('schoolInfo.id');
		    $data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
		    $validate = new DemeanorValidate;
		    if (!$validate->scene('editVideo')->check($data)) {
		        return show(0, $validate->getError(), 0, 200);
		    } else {
		        Cache::clear('campus_elegance');
                $rest = (new DemeanorModel())->strict(false)->where('id',$data['id'])->update($data);
                if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
		        return show(1, '添加成功', $rest, 200);
		    }
		}
	    $data = request()->param();
	    $result = db('campus_elegance')->where('id', '=', $data['id'])->find();
	    $this->assign('result', $result);
		$egetClass = $this->egetClass($result['class_ids']);
		$this->assign('egetClass', json_encode($egetClass['data']));
	    return $this->fetch();
	}
    //添加
    public function add()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $data['created_at'] = date('Y-m-d H:i:s', time());
            $data['user_id'] = session('admin.id');
            $data['school_id'] = session('schoolInfo.id');
            $data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
			$data['images'] =  implode(",", $data['images']);
            $validate = new DemeanorValidate;
            if (!$validate->scene('add')->check($data)) {
                return show(0, $validate->getError(), 0, 200);
            } else {
                Cache::clear('campus_elegance');
                $rest = (new DemeanorModel())->strict(false)->insert($data);
                if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
                return show(1, '添加成功', $rest, 200);
            }
        }
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
        return $this->fetch();
    }
    public function addVideo()
    {
        if (request()->isPost()) {
            $data = request()->param();
            $data['created_at'] = date('Y-m-d h:i:s', time());
            $data['user_id'] = session('admin.id');
            $data['school_id'] = session('schoolInfo.id');
            $data['platform'] = 1;
			$data['class_ids'] =  implode(",", $data['class_ids']);
            $validate = new DemeanorValidate;
            if (!$validate->scene('addVideo')->check($data)) {
                return show(0, $validate->getError(), 0, 200);
            } else {
                Cache::clear('campus_elegance');
                $rest = (new DemeanorModel())->strict(false)->insert($data);
                if($rest){
					//如果发布信息成功推送到相对于的设备上进行更新相对应的模块后期加入log日志判断是否发送成功
					$this->socketPush($data['class_ids'],$this->pushKey);
				}
                return show(1, '添加成功', $rest, 200);
            }
        }
		$egetClass = $this->getClass();
		$this->assign('egetClass', json_encode($egetClass['data']));
        return $this->fetch();
    }
    
    //批量删除
    public function delete()
    {
        Cache::clear('campus_elegance');
        $data = request()->param();
        $eleganceInfo = db('campus_elegance')->where(['id'=>$data['ids']])->where(['user_id'=>$data['user_id']])->select();
        $result = array_column($eleganceInfo,'class_ids');
        $result = implode(',', $result);
        $result = explode(',', $result);
        $class_ids = array_unique($result);
        $this->socketPush($class_ids,$this->pushKey);
        $result = db('campus_elegance')->where(['user_id'=>$data['user_id']])->delete($data['ids']);
        return show(1, '删除成功', $result, 200);
    }
    //是否显示
    public function show()
    {
        $data = request()->param();
        Cache::clear('campus_elegance');
        $result = db('campus_elegance')->where('id', $data['id'])->update(['show'=>$data['status']]);
        $newInfo = db('campus_elegance')->where(['id'=>$data['id']])->find();
        $class_ids = explode(',',  $newInfo['class_ids']);
        $this->socketPush($class_ids,$this->pushKey);
        return show(1, '设置成功', $result, 200);
    }
    //是否置顶
    public function top()
    {
        $data = request()->param();
        Cache::clear('campus_elegance');
        $result = db('campus_elegance')->where('id', $data['id'])->update(['istop'=>$data['status']]);
        $newInfo = db('campus_elegance')->where(['id'=>$data['id']])->find();
        $class_ids = explode(',',  $newInfo['class_ids']);
        $this->socketPush($class_ids,$this->pushKey);
        return show(1, '设置成功', $result, 200);
    }
    //排序
    public function sort()
    {
        $data = request()->param();
        
        $validate = new DemeanorValidate;
        
        if (!$validate->scene('sort')->check($data)) {
            return show(config('code.error'), 'error', $validate->getError(), 200);
        } else {
            Cache::clear('campus_elegance');
            $rest = (new DemeanorModel())->save(['sort'=>$data['sort']], ['id'=>$data['id']]);
            return show(config('code.success'), 'OK', $rest, 200);
        }
    }
}
